#!usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on 20230926
Author:   yuyantao
Function: acheive the conversion of excel to function covergroup
"""
#open the excel
import xlwings as xw
#import xlrd as xw
wb    = xw.Book("E:\Python\jiaobenlianxi\INSTR.xlsx")
receive = wb.sheets["receive"]
print("The excel has been opened")

#define the name of row
column_signal = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
column_signal.extend(['AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'])
column_signal.extend(['BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ','BK','BL','BM','BN'])

#Acquire the datetime
import datetime as dt
now_time = dt.datetime.now().strftime('%F %T')
print("The datetime of the RTL created: "+now_time )

#Acquire the name of the module
Module_name = ""
Module_name = receive.range(column_signal[5] + str(2)).value
print("The module name: "+Module_name)
 
#Acquire the number of the coverpoint bins
row_max= 0
temp = "temp"
while(1):
    row_max = row_max + 1
    temp = receive.range(column_signal[0] + str(row_max)).value
    if(temp == None):
        break
row_max = row_max - 1
print("The valid range of the data of the sheets: 1-"+str(row_max))
 
#Acquire the name of the covergroup
Group_name = []
for i in range(2,row_max+1):
    temp = receive.range(column_signal[1] + str(i)).value
    Group_name.append(temp)
print("The name of the covergroup: " + str(Group_name))
 
#Acquire the name of the coverpoint
Coverpoint = []
for i in range(2,row_max+1):
    temp = receive.range(column_signal[2] + str(i)).value
    Coverpoint.append(temp)
print("The name of the coverpoint" + str(Coverpoint))

#Acquire the name of the coverpoint bins
Bins_name = []
for i in range(2,row_max+1):
    temp = receive.range(column_signal[3] + str(i)).value
    Bins_name.append(temp)
print("The name of bins" + str(Bins_name))

#Acquire the value of the coverpoint bins
Bins_value = []
for i in range(2,row_max+1):
    temp = receive.range(column_signal[4] + str(i)).value
    Bins_value.append(temp)
print("The value of bins" + str(Bins_value))
 
#Command_Type_binary = []
#for i in range(2,row_max+1):
#    temp = receive.range(column_signal[4] + str(i)).value
#    Command_Type_binary.append(temp)
#print("指令类型列表：" + str(Command_Type_binary))

#write the code of the covergroup
with open(Module_name + ".sv","w") as f:
    f.write("`timescale 1ns / 1ps \n")
    f.write("// Create Date: " + str(now_time) + "\n")
    f.write("// Module Nmae: " + Module_name + "\n" )
    f.write("// Description: Covergroup of RV Instr" + "\n")
    f.write("module " + str(Module_name)+"(input clk input rst_n input [SIZE-1:0] opcode);\n")
    cnt = 0
    for i in range(0,len(Group_name)-1):
        if ((Group_name[i] == Group_name[i+1])):
            cnt = cnt + 1
            f.write("   `define " + str(Bins_name[i]) + " = `SIZE'b" + str(Bins_value[i])+ "\n")
            if((i==len(Group_name)-2)):
                f.write("   `define " + str(Bins_name[i+1]) + " = `SIZE'b" + str(Bins_value[i+1])+ "\n")
                f.write("\n")
                f.write("   covergroup cg_" + str(Group_name[i] + " @(posedge clk);\n"))
                f.write("       type_option.weight = 1;\n")
                f.write("       RV32_" + Coverpoint[i]+": coverpoint opcode{\n")
                for j in range(i-cnt+1,i+2):
                    f.write("           wildcard bins RV32_" + Bins_name[j] + "_PC = " + "{`" + Bins_name[j] + "};\n")
                f.write("           bins others = {default};\n")
                f.write("       }\n")
                f.write("   endgroup\n")
                cnt = 0
                f.write("\n")
        elif((Group_name[i] != Group_name[i+1])):
            f.write("   `define " + str(Bins_name[i]) + " = `SIZE'b" + str(Bins_value[i])+ "\n")
            f.write("\n")
            f.write("   covergroup cg_" + str(Group_name[i] + " @(posedge clk);\n"))
            f.write("       type_option.weight = 0;\n")
            f.write("       RV32_" + Coverpoint[i]+": coverpoint opcode{\n")
            for j in range(i-cnt+1,i+1):
                f.write("           wildcard bins RV32_" + Bins_name[j] + "_PC = " + "{`" + Bins_name[j] + "};\n")
            f.write("           bins others = {default};\n")
            f.write("       }\n")
            f.write("   endgroup\n")
            cnt = 0
            f.write("\n")

    #CoverGroup Instantiation
    for i in range(0,len(Group_name)-1):
        if((Group_name[i] != Group_name[i+1]) | (i==len(Group_name)-2)):
            f.write("   cg_" + str(Group_name[i]) + " cg_" + str(Group_name[i])+ "_inst;\n")
    f.write("\n")
    f.write("   initial begin\n")
    for i in range(0,len(Group_name)-1):
        if((Group_name[i] != Group_name[i+1]) | (i==len(Group_name)-2)):
            f.write("       cg_" + str(Group_name[i])+ "_inst = new();\n")
    f.write("   end\n")
    f.write("\n")
    f.write("endmodule\n")
